package app;

import java.util.ArrayList;
import java.util.Iterator;

import organisms.Tracer;

import processing.core.PApplet;
import field.AngleField;
import geom.Line;
import geom.PolarVector;
import geom.Segment;
import geom.Vector;


public class Fielders extends PApplet{
	ArrayList<Tracer> tracers;
	AngleField field;
	int width = 1440;
	int height = 900;
	
	static public void main(String args[]) {
		PApplet.main(new String[] { 

			"--present",
			"app.Fielders" 
		});
	}
	
	public void setup(){
		frameRate(50);
		size(width, height);
		smooth();
		tracers = new ArrayList<Tracer>();
		field = new AngleField(new Vector(width, height, 0));
		background(0);
	}
	
	public void keyPressed(){
		if(key == 'a'){
			addTracer();
		}
		if(key == 'A'){
			for(int i = 0; i < 25; i++){
				addTracer();
			}
		}
		if(key == 'b'){
			drawBins = !drawBins;
		}
		if(key == ENTER){
			println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		}
		if(key == 't'){
			scope = !scope;
		}
		if(key == 'c'){
			background(0);
			tracers = new ArrayList<Tracer>();
			field = new AngleField(new Vector(width, height, 0));
		}
			
	}
	
	boolean scope = false;
	public void drawFieldScope(Vector x, float m){
		float size = 300;
		float res = 30;
		float length = size / res;
		
		for(int i = 0; i < res; i++){
			for(int k = 0; k < res; k++){
				Vector pos = new Vector((i+1)*length, (k+1)*length, 0);
				Vector sample = x.sub(new Vector(size/2f, size/2f, 0)).add(pos.mult(1f/m));
				if(i == 0 || i == res - 1 || k == 0 || k == res -1);
				//Vector f = field.value(sample, this);
				ellipse(pos.x, pos.y, 2, 2);
				//Vector epos = pos.add(f.mult(7));
				//line(pos.x, pos.y, epos.x, epos.y);
			}
		}
		
		
		
		
		
	}
	
	public void mousePressed(){
		drawSegments = true;
		if(mousePath != null){
			mousePaths.add(mousePath);
		}
		mousePath = new Line(this);
	}
	
	public void mouseReleased(){
		drawSegments = false;
	}
	
	public void mouseDragged(){
		Vector pos = new Vector(mouseX, mouseY, 0);
		mousePath.addPoint(pos);
		if(mousePath.length() > 1){
			field.add(new Segment(mousePath, mousePath.size()-2, mousePath.size()-1));
		}
		mousePath.draw();
	}
	boolean drawSegments = false;
	ArrayList<Line> mousePaths = new ArrayList<Line>();
	Line mousePath;

	
	
	public void addTracer(){
		float dist = (float)Math.sqrt(Math.pow(height/2f, 2) + Math.pow(width/2f, 2));
		PApplet.println("addtracer");
		Vector pos = new PolarVector(random(2 * PI), 0,  dist).getCartesian().add(new Vector(width /2f, height/2f, 0));
		Vector speed = new Vector(random(width), random(height), 0).sub(pos).normalize().mult(8);
		PApplet.println("pos speed " + pos + " ;;; " + speed);
		Tracer tracer = new Tracer((PApplet)this, field, pos, speed);
		tracers.add(tracer);
		
		PApplet.println("addedd tracer");
	}
	boolean drawBins = false;
	public void draw(){
		
		//
		if(drawBins) field.draw(this);
		Iterator<Tracer> it = tracers.iterator();
		while(it.hasNext()){
			Tracer next = it.next();
			if(!scope && !drawSegments){
				next.move();
			}
			next.drawTrace();
			
		}
		
		if(scope){
			drawFieldScope(new Vector(mouseX, mouseY, 0), 10);
		}
	}
}
